﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace yq.BLOB
{
    public class File
    {
        yq.BLOB.SysFileServer Server = new SysFileServer();

        string FolderId = "";

        /// <summary>
        /// 创建和获取文件夹信息
        /// </summary>
        /// <param name="folderId"></param>
        public File(string folderId)
        {
            FolderId = folderId;
            var info = Folder.Get(folderId);
            if (info.IsLocd)
            {
                Server = info;
            }
            else if (Folder.Add(folderId))
            {
                Server = Folder.Get(folderId);
            }
        }

        /// <summary>
        /// 添加文本
        /// </summary>
        /// <param name="txtname"></param>
        /// <param name="txt"></param>
        /// <returns></returns>
        public string AddText(string txtname, string txt)
        {
            var web = new cn.Util.WebApi();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("folderId", FolderId);
            parameters.Add("filename", txtname);
            parameters.Add("extension", "txt");

            Encoding encoding = System.Text.Encoding.UTF8;
            byte[] buffer = encoding.GetBytes(txt);

            var msg = web.ToFile(Server.Url + "File/Add", buffer, parameters);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(msg.Text);
        }

        /// <summary>
        /// 添加文件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="extension"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public string Add(string name, string extension, byte[] file)
        {
            var web = new cn.Util.WebApi();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("folderId", FolderId);
            parameters.Add("filename", name);
            parameters.Add("extension", extension);

            var msg = web.ToFile(Server.Url + "File/Add", file, parameters);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<string>(msg.Text);
        }

        /// <summary>
        /// 获取文件
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public byte[] Get(string fileid)
        {
            var web = new cn.Util.WebApi();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("fileid", fileid);
            var msg = web.To(Server.Url + "File/Get", "", parameters);
            return Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]>(msg.Text);
        }

        /// <summary>
        /// 获取文件
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public string GetText(string fileid)
        {
            var web = new cn.Util.WebApi();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("fileid", fileid);
            var msg = web.To(Server.Url + "File/Get", "", parameters);
            var by = Newtonsoft.Json.JsonConvert.DeserializeObject<byte[]>(msg.Text);
            return System.Text.Encoding.UTF8.GetString(by);
        }

        /// <summary>
        /// 获取文件访问地址
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public string GetUrl(string fileid)
        {
            var files = fileid.Split(char.Parse("_"));
            var info = Folder.Get(files[0]);

            return info.WebPath + "/" + files[1] + "/" + files[2] + "/" + fileid;
        }

        /// <summary>
        /// 获取文件访问地址 静态
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public static string GetFileUrl(string fileid)
        {
            var files = fileid.Split(char.Parse("_"));
            var info = Folder.Get(files[0]);

            return info.WebPath + "/" + files[1] + "/" + files[2] + "/" + fileid;
        }

        /// <summary>
        /// 获取文件访问地址
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public string GetUrlVer(string fileid)
        {
            if (string.IsNullOrEmpty(fileid) == false)
            {
                var web = new cn.Util.WebApi();
                Dictionary<string, string> parameters = new Dictionary<string, string>();
                parameters.Add("fileid", fileid);
                var msg = web.To(Server.Url + "File/Ver", "", parameters);
                var files = fileid.Split(char.Parse("_"));
                var info = Folder.Get(files[0]);
                string ver = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(msg.Text);
                return info.WebPath + "/" + files[1] + "/" + files[2] + "/" + fileid + "?ver=" + ver;
            }
            else
            {
                return "";
            }
        }


        /// <summary>
        /// 直接修改文本
        /// </summary>
        /// <param name="fileid"></param>
        /// <param name="txt"></param>
        /// <returns></returns>
        public bool EditText(string fileid, string txt)
        {
            var web = new cn.Util.WebApi();

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("fileid", fileid);
            parameters.Add("extension", "txt");
            Encoding encoding = System.Text.Encoding.UTF8;
            byte[] buffer = encoding.GetBytes(txt);
            var msg = web.ToFile(Server.Url + "File/Edit", buffer, parameters);
            return msg.IsOk;
        }

        /// <summary>
        /// 修改文件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        public bool Edit(string id, byte[] file)
        {
            var web = new cn.Util.WebApi();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("fileid", id);
            var msg = web.ToFile(Server.Url + "File/Edit", file, parameters);

            return msg.IsOk;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileid"></param>
        /// <returns></returns>
        public bool Del(string fileid)
        {
         
            var web = new cn.Util.WebApi();
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("fileid", fileid);
            return web.To(Server.Url + "File/Del", "", parameters).IsOk;
        }

        //public char subString = '?';

        ///// <summary>
        ///// 省略?号后面的ID
        ///// </summary>
        ///// <param name="fileid"></param>
        ///// <returns></returns>
        //private string subId(string fileid)
        //{
        //    return fileid.Split(subString)[0];
        //}

        ///// <summary>
        ///// 省略?号后面的ID
        ///// </summary>
        ///// <param name="fileid"></param>
        ///// <returns></returns>
        //public string NewFileId(string fileid)
        //{
        //    return fileid.Split(subString)[0] + subString + DateTime.Now.Ticks.ToString();
        //}
    }
}
